<template>
	<view class="inquiryOrderInfo-wrap" v-if="commodityOrder">
		<view class="drugDescribe-box1" v-if="commodityOrder.orderType == 2">
			<u-title value="开药问诊流程"></u-title>

			<view class="drugDescribe-icon-list">
				<view class="drugDescribe-icon-item">
					<image src="http://k8s.vendingtech.vip/images/data_icon@2x.png" class="pic"></image>
					<view class="txt2">患者填写病情</view>
				</view>

				<view class="drugDescribe-icon-item">
					<image src="http://k8s.vendingtech.vip/images/prescription_icon@2x.png" class="pic"></image>
					<view class="txt2">医生问诊开处方</view>
				</view>

				<view class="drugDescribe-icon-item">
					<image src="http://k8s.vendingtech.vip/images/prescription_icon_yellow@2x.png" class="pic"></image>
					<view class="txt2">药师审核处方</view>
				</view>

				<view class="drugDescribe-icon-item">
					<image src="http://k8s.vendingtech.vip/images/deliver_icon@2x.png" class="pic"></image>
					<view class="txt2">线上购药发货</view>
				</view>

				<view class="mod2"></view>
			</view>
		</view>

		<view class="inquiryOrderInfo-price-mod">
			<u-title value="付款详情"></u-title>
			<view class="inquiryOrderInfo-price-num">
				<view class="word3">{{ orderTypeList[commodityOrder.orderType].name }}-{{
						inquiryTypeList[commodityOrder.inquiryType].name
				}}</view>
				<!-- <view class="txt1">¥&nbsp;{{ commodityOrder.totalAmount }}</view> -->
				<!-- 待支付 -->
				<view class="info2 info2-red" v-if="commodityOrder.status == 3">{{
						orderStatusList[commodityOrder.status]
				}}</view>
				<!-- 待接诊 -->
				<view class="info2 info2-red" v-else-if="commodityOrder.status == 1 && questionOrder.status == 0">{{
						qStatusList[questionOrder.status]
				}}</view>

				<!-- 问诊中 -->
				<view class="info2 info2-green" v-else-if="questionOrder.status == 1">{{
						qStatusList[questionOrder.status]
				}}</view>
				<!-- 已开处方 -->
				<view class="info2 info2-green" v-else-if="questionOrder.status == 2">{{
						qStatusList[questionOrder.status]
				}}</view>
				<!-- 已退诊 -->
				<view class="info2 info2-gray" v-else-if="questionOrder.status == 4">{{
						qStatusList[questionOrder.status]
				}}</view>
				<!-- 退款中|已退款 -->
				<view class="info2 info2-gray" v-else-if="commodityOrder.status == 4 || commodityOrder.status == 5">{{
						orderStatusList[commodityOrder.status]
				}}</view>
				<!-- 待发货|待收货 -->
				<view class="info2 info2-purple" v-else-if="commodityOrder.status == 7 || commodityOrder.status == 8">{{
						orderStatusList[commodityOrder.status]
				}}</view>
				<!-- 已完成 -->
				<view class="info2 info2-gray" v-else-if="commodityOrder.status == 1 || questionOrder.status == 3">{{
						qStatusList[questionOrder.status]
				}}</view>
				<!-- 其他状态 -->
				<view class="info2 info2-purple" v-else>{{ orderStatusList[commodityOrder.status] }}</view>
			</view>

			<u-countDown
				v-if="commodityOrder.status == 1 && questionOrder.status == 0 && endDatatime > new Date().getTime() / 1000"
				:datatime="endDatatime" @endTime="questionOrderDetail"></u-countDown>

			<view class="inquiryOrderInfo-price-num" v-if="questionOrder.subTime">
				<view class="word3">预约时间</view>
				<view class="txt1">{{ questionOrder.subTime }}</view>
			</view>

			<view class="inquiryOrderInfo-doctor" v-if="doctorId">
				<u-title value="医生详情"></u-title>
				<u-doctor-item :data="doctorDetail" :type="1"></u-doctor-item>
			</view>
		</view>

		<view class="inquiryOrderInfo-drug" v-if="commodityOrder.orderType == 2 && questionOrder.drugQuestionReq">
			<u-title value="已选药品"></u-title>
			<view class="inquiryOrderInfo-drug-list">
				<u-show-goods v-for="(item, index) in questionOrder.drugQuestionReq.commodityList" :key="index"
					:data="item"></u-show-goods>
			</view>
		</view>

		<view class="inquiryOrderInfo-patient">
			<u-title value="问诊信息"></u-title>
			<view class="inquiryOrderInfo-patient-box">
				<view class="inquiryOrderInfo-patient-outer">
					<text lines="1" class="word9">患者信息：</text>
					<text lines="1" class="word10">{{ patient.patientName }}&nbsp;&nbsp;{{ rangeSex[patient.sex].text
					}}&nbsp;&nbsp;{{ patient.phone }}</text>
				</view>
				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">所患疾病：</text>
					<text lines="1" class="word10">{{ questionOrder.content }}</text>
				</view>

				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">肝功能：</text>
					<text lines="1" class="word10">{{ functionList[questionOrder.drugQuestionReq.liver].name }}</text>
				</view>

				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">肾功能：</text>
					<text lines="1" class="word10">{{ functionList[questionOrder.drugQuestionReq.kidney].name }}</text>
				</view>

				<view class="inquiryOrderInfo-patient-outer"
					v-if="commodityOrder.orderType == 2 && questionOrder.drugQuestionReq.allergy == 1">
					<text lines="1" class="word9">过敏史：</text>
					<text lines="1" class="word10">{{ questionOrder.drugQuestionReq.allergyText }}</text>
				</view>

				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType == 2">
					<text lines="1" class="word9">生育状态及计划：</text>
					<text lines="1" class="word10">{{ birthStatusList[questionOrder.drugQuestionReq.birthStatus].name
					}}</text>
				</view>

				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType != 2">
					<text lines="1" class="word9">患病时间：</text>
					<text lines="1" class="word10">{{ durationList[questionOrder.duration].name }}</text>
				</view>
				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType != 2">
					<text lines="1" class="word9">就诊情况：</text>
					<text lines="1" class="word10">{{ isLookHospList[questionOrder.isLookHosp].name }}</text>
				</view>
				<view class="inquiryOrderInfo-patient-outer" v-if="commodityOrder.orderType != 2">
					<text lines="1" class="word9">病情描述：</text>
					<text lines="1" class="word10">{{ questionOrder.content }}</text>
				</view>
			</view>
		</view>

		<view class="inquiryOrderInfo-report" v-if="reportList && reportList.length > 0">
			<u-title :value="commodityOrder.orderType == 2 ? '处方证明' : '检查报告或患处证明'"></u-title>
			<view class="inquiryOrderInfo-report-list">
				<image :src="item" class="inquiryOrderInfo-report-item" v-for="(item, index) in reportList"
					@click="previewImage(reportList, index)" :key="index"></image>
			</view>
		</view>
		
		<view class="inquiryOrderInfo-report" v-if="questionOrder.reexamination">
			<u-title value="复诊文件（PDF格式文件）"></u-title>
			<view class="inquiryOrderInfo-report-list">
				<view class="inquiryPayment-report-list">{{questionOrder.reexamination.split('/').pop()}}</view>
			</view>
		</view>

		<view class="inquiryOrderInfo-patient">
			<u-title value="订单信息"></u-title>
			<view class="inquiryOrderInfo-patient-box">
				<view class="inquiryOrderInfo-patient-outer">
					<text lines="1" class="word10">订单编号：{{ commodityOrder.id }}</text>
					<view class="word-btn" @click="setClipboardData(commodityOrder.id)">复制</view>
				</view>
				<view class="inquiryOrderInfo-patient-outer">
					<text lines="1" class="word10">下单时间：{{ commodityOrder.orderTime }}</text>
				</view>
				<view class="inquiryOrderInfo-patient-outer">
					<text lines="1" class="word10">订单金额：{{ commodityOrder.totalAmount }} 元</text>
				</view>
				<view class="inquiryOrderInfo-patient-outer">
					<text lines="1" class="word10">优惠券：{{ commodityOrder.couponMoney || '0.00' }} 元</text>
				</view>
				<view class="inquiryOrderInfo-patient-outer">
					<text lines="1" class="word10">支付金额：{{ commodityOrder.orderAmount || '0.00' }} 元</text>
				</view>
			</view>
		</view>

		<u-bottom-button type="3" value1="取消订单" value2="立即支付" @clickOne="clearOrder" @clickTwo="submitOrder"
			v-if="commodityOrder.status == 3"></u-bottom-button>
		<!--
		<u-bottom-button
			type="3"
			value2="继续咨询"
			@clickTwo="gotoRouter('/pages-im/chatRoom/chatRoom?accid=' + doctorId + '&role=0&questionOrderId='+questionOrder.id)"
			v-if="commodityOrder.status == 1 && questionOrder.status == 1 && showBtn"
		></u-bottom-button>
		-->
		<u-bottom-button type="3" value1="查看处方" value2="继续咨询" @clickTwo="continueInquery" @clickOne="checkPrescription()"
			v-else-if="commodityOrder.status == 1 && (questionOrder.status == 1 || questionOrder.status == 2) && showBtn">
		</u-bottom-button>
		
		<u-bottom-button type="3" value1="查看处方" value2="立即购药" @clickOne="checkPrescription()"
			@clickTwo="gotoRouter('/pages-mall/orderCreate/orderCreate?orderCode=' + questionOrder.drugOrderCode)"
			v-else-if="questionOrder.status == 2 && userRecipe.status == 1 && showBtn"></u-bottom-button>

		<u-bottom-button type="3" value1="查看处方" value2="去评价" @clickOne="checkPrescription()"
			@clickTwo="gotoRouter('/pages-member/evaluate/evaluate?doctorId=' + questionOrder.doctorId + '&orderId=' + commodityOrder.id)"
			v-else-if="commodityOrder.status == 1 && questionOrder.status == 3 && showBtn"></u-bottom-button>
			
		<u-bottom-button type="3" value1="查看处方" @clickOne="checkPrescription()"
			v-else="questionOrder.status == 2 && userRecipe.status != 1 && showBtn"></u-bottom-button>
	</view>
</template>

<script>
import uTitle from '@/components/u-title/u-title.vue';
import uShowGoods from '@/components/u-show-goods/u-show-goods.vue';
import uBottomButton from '@/components/u-bottom-button/u-bottom-button.vue';
import uDoctorItem from '@/components/u-doctor-item/u-doctor-item.vue';
import uCountDown from '../components/u-countDown/u-countDown';
import { currentImInfo } from '@/utils/token.js'
const app = getApp();
export default {
	components: {
		uBottomButton,
		uTitle,
		uDoctorItem,
		uShowGoods,
		uCountDown
	},
	data() {
		return {
			id: null, //问诊订单id
			doctorId: null,
			doctorDetail: {},
			patient: null, //患者
			commodityOrder: null, //订单信息
			questionOrder: null, //问诊详情
			reportList: [],
			userRecipe: null, //处方信息
			showBtn: true,
			endDatatime: '', // 结束问诊时间
			intervalId: 0
		};
	},

	onLoad(option) {
		this.id = option.id;
		this.showBtn = option.showBtn == 0 ? false : true;
	},
	onShow() {
		this.questionOrderDetail();
		this.intervalId = setInterval(() => {
			this.questionOrderDetail();
		}, 5000);
		console.log("intervalId ====>>>> " + this.intervalId);
	},
	onUnload() {
		clearInterval(this.intervalId);
	},
	methods: {
		//医生详情
		queryDoctorDetail() {
			this.$api
				.queryDoctorDetail({
					params: {
						id: this.doctorId
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.doctorDetail = res.result;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//订单详情
		questionOrderDetail() {
			this.$api
				.questionOrderDetail({
					params: {
						id: this.id
					}
				})
				.then(res => {
					if (res.code == 200) {
						if (res.result) {
							this.commodityOrder = res.result.commodityOrder;
							this.patient = res.result.patient;
							this.questionOrder = res.result.questionOrder;
							this.userRecipe = res.result.userRecipe;
							this.doctorId = res.result.questionOrder.doctorId;
							if (this.doctorId) {
								this.queryDoctorDetail();
							}

							this.reportList = res.result.questionOrder.reportList;

							this.endDatatime = this.questionOrder.validPeriod ? new Date(this.questionOrder.validPeriod.replace(/\-/g, '/')).getTime() / 1000 + 10 : '';

							// console.log(this.endDatatime);
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//取消订单
		clearOrder() {
			uni.showModal({
				title: '确认要取消该订单吗？',
				success: res => {
					if (res.confirm) {
						this.updateOrderStatus(6);
					}
				}
			});
		},

		//发起微信支付
		submitOrder() {
			this.$api
				.submitOrder({
					params: {
						commodityName: this.orderTypeList[this.commodityOrder.orderType].name + '-' + this.inquiryTypeList[this.commodityOrder.inquiryType].name,
						orderId: this.commodityOrder.id
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.wxRequestPayment(res.result);
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//微信小程序拉起支付
		wxRequestPayment(res) {
			wx.requestPayment({
				timeStamp: res.timeStamp,
				nonceStr: res.nonceStr,
				package: res.packageValue,
				signType: res.signType,
				paySign: res.paySign,
				success: success => {
					uni.showToast({
						title: '支付成功',
						icon: 'none'
					});
					this.questionOrderDetail();
				},
				fail: error => {
					uni.showToast({
						title: '取消支付',
						icon: 'none'
					});
				},
				complete: () => {
					this.payFlag = true;
				}
			});
		},
		//查看处方
		checkPrescription() {
			if (this.userRecipe && this.userRecipe.id) {
				this.$api
					.getUserRecipeDetail({
						params: {
							id: this.userRecipe.id
						}
					})
					.then(res => {
						if (res.code == 200) {
							if(res.result && res.result.status == 0){
								uni.showToast({
									title: '该处方未审核，不支持查看',
									icon: 'none'
								});
							}else{
								this.gotoRouter('/pages-doctor/prescInfo/prescInfo?id=' + this.userRecipe.id + '&role=0')
							}
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							});
						}
					});
			} else {
				uni.showToast({
					title: '医生未开处方',
					icon: 'none'
				});
			}
		},
		//修改订单状态
		updateOrderStatus(status) {
			this.$api
				.updateOrderStatus({
					params: {
						orderId: this.commodityOrder.id,
						status: status
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.questionOrderDetail();
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},
		continueInquery() {
			// gotoRouter('/pages-txy-im/chatRoom/chatRoom?accid=' + doctorId + '&role=0&questionOrderId='+questionOrder.id)
			uni.navigateTo({
				url: '/pages-txy-im/chatRoom/chatRoom?accid=' + this.doctorId + '&role=0&questionOrderId=' + this.questionOrder.id
			})
		}
	},

};
</script>

<style lang="less">
@import './inquiryOrderInfo.less';
</style>
